library Wave0 initializer init requires LegacySystem

    globals
        private trigger CivilianAITrigger
		private trigger CivilianAIFlee
		private trigger CivilianAIShotAt
		private group G = null
		
		private integer array ShotAtArrayCounter
		private boolean array Art15Given
		private integer ShotAtGlobalCounter = 0
	endglobals

	private function CleanupLeaks takes nothing returns nothing
        call DisableTrigger(CivilianAITrigger)
		call DisableTrigger(CivilianAIFlee)
		call DisableTrigger(CivilianAIShotAt)
    endfunction
	
	private function GiveArt15ToAllOffenders takes nothing returns nothing
		local integer i = 0
		loop
			exitwhen i > 12
			if Art15Given[i] == false and ShotAtArrayCounter[i] >= 3 then
				set Art15Given[i] = true
				call GiveArticle15(Player(i), 0.95, GetPlayerName(Player(i)) + " has opened fire at civilians.")
			endif			
			set i = i + 1
		endloop
	endfunction
	
	private function MassFleeE takes nothing returns boolean
		local unit u = GetFilterUnit()
		call IssuePointOrderById(u, ORDER.move, GetRandomReal(-5000.0, 10000.0), GetRandomReal(-5000.0, 10000.0))
		if GetRandomInt(0,6)==1 then
			call SimpleChat(u,2.0, GetUnitName(u) + " : They are gonna kill us all!" )
		elseif GetRandomInt(0,6)==1 then
			call SimpleChat(u,2.0, GetUnitName(u) + " : They are shooting us, run for your lives!" )
		endif
		set u = null
		return false
	endfunction
	
	private function MassFleeRemove takes nothing returns boolean
		call RemoveUnit(GetFilterUnit())
		return false
	endfunction
	
	private function MassFlee takes nothing returns nothing
		call GroupEnumUnitsOfPlayer(G,Player(9),Condition(function MassFleeE))
		call CleanupLeaks()
		call PolledWait(20.0)
		call GroupEnumUnitsOfPlayer(G,Player(9),Condition(function MassFleeRemove))
	endfunction
	
	private function CivilianAIShotAtActions takes nothing returns boolean
		local unit u = GetTriggerUnit()
		local unit a = null
		local player p = GetOwningPlayer(GetAttacker())
		local integer i = GetPlayerId(p)
		
		if udg_CurrentWave <= 3 and GetOwningPlayer(u) == Player(9) and i < 12 then
			set a = GetAttacker()
			
			if GetRandomInt(0,3)==1 then
				call SimpleChat(u,2.0, GetUnitName(u) + " : Arghh!" )
			elseif GetRandomInt(0,3)==1 then
				call SimpleChat(u,2.0, GetUnitName(u) + " : Nooo!" )
			elseif GetRandomInt(0,3)==1 then
				call SimpleChat(u,2.0, GetUnitName(u) + " : Please, don't shoot!" )
			endif
			
			if ShotAtGlobalCounter >= 4 then
				call GiveArt15ToAllOffenders()
			endif
			
			if ShotAtGlobalCounter == 7 then
				call MassFlee.execute()
			endif
			
			set ShotAtGlobalCounter = ShotAtGlobalCounter + 1
			set ShotAtArrayCounter[i] = ShotAtArrayCounter[i] + 1
			set a = null
		endif

		set p = null
		set u = null
		return false
	endfunction
	
	private function CivilianAIFleeAction takes nothing returns nothing
		local unit u = GetTriggerUnit()
		if GetOwningPlayer(u) == Player(9) and udg_CurrentWave <= 3 then
			call IssuePointOrderById(u, ORDER.move, -1100.0 + GetRandomReal(-150.0,150.0), -310.0 + GetRandomReal(-150.0,150.0))
			call TriggerSleepAction(1.0)
			call IssuePointOrderById(u, ORDER.move, -1100.0 + GetRandomReal(-150.0,150.0), -310.0 + GetRandomReal(-150.0,150.0))
			call TriggerSleepAction(1.0)
			call IssuePointOrderById(u, ORDER.move, -1100.0 + GetRandomReal(-150.0,150.0), -310.0 + GetRandomReal(-150.0,150.0))
			call TriggerSleepAction(1.0)
			call IssueImmediateOrderById(u, ORDER.stop)
		endif
		set u = null
	endfunction
	
    private function CivilianAI takes nothing returns nothing
        local unit u
        local group g
		
		set g = G
		
        call GroupEnumUnitsOfPlayer(g,Player(9),null)
        loop
            set u = FirstOfGroup(g)
            exitwhen u==null
            call TriggerSleepAction(0.)
            call ItemAI(u)
            if GetWidgetLife(u)<=GetUnitState(u,UNIT_STATE_MAX_LIFE)/1.75 and IsUnitType(u,UNIT_TYPE_MECHANICAL)!=true then
			
				if GetRandomInt(0,3)==1 then
					if GetRandomInt(0,1)==1 then
						call SimpleChat(u,2.0, GetUnitName(u) + " : I'm injured!" )
					elseif GetRandomInt(0,1)==1 then
						call SimpleChat(u,2.0, GetUnitName(u) + " : Help me!" )
					elseif GetRandomInt(0,1)==1 then
						call SimpleChat(u,2.0, GetUnitName(u) + " : It hurts.." )
					else
						call SimpleChat(u,2.0, GetUnitName(u) + " : I don't want to die.." )
					endif
				endif

				if udg_CurrentWave <= 3 and GetUnitAbilityLevel(u, 'Abun') == 0 and GetWidgetLife(u)<=GetUnitState(u,UNIT_STATE_MAX_LIFE)/1.75 then
					call IssuePointOrderById(u, ORDER.move, -1100.0 + GetRandomReal(-150.0,150.0), -310.0 + GetRandomReal(-150.0,150.0))
					call UnitAddAbility(u, 'Abun')
				endif
            endif
            call GroupRemoveUnit(g,u)
        endloop
		call GroupClear(g)
        set g = null
    endfunction


    
    private function Wave0 takes nothing returns nothing
        local unit u
        local integer i = 0
		local real x 
		local real y

		set x = -710.0
		set y = -115.0
        if udg_CurrentWave==-1 then
            set udg_CurrentWave=0
        else
            return
        endif
        if udg_InitialPlayers<3 then
            call SetDynamicEvasionEnabled(true)
            set udg_GlobalUnit[100] = CreateUnit(Player(11),'z008',57.0,150.0,0.0)
            call SetLoadState(I2S(GetHandleId(udg_GlobalUnit[100])),1)
            call TriggerSleepAction(0.)
            if udg_InitialPlayers==1 then
                set udg_GlobalUnit[101] = CreateUnit(Player(11),'z008',-1251.0,348.0,0.0)
                call SetLoadState(I2S(GetHandleId(udg_GlobalUnit[101])),1)
            endif
        endif
        
		set ShotAtGlobalCounter = 0
		call EnableTrigger(CivilianAITrigger)
		call EnableTrigger(CivilianAIFlee)
		call EnableTrigger(CivilianAIShotAt)
		
		
        call CameraSetTargetNoiseForPlayer(GetLocalPlayer(),4.0,0.02)
        call PolledWait(8.0*udg_Pace)
        call MultiboardSetTitleText(udg_SquadStatus, "Insertion in Progress.." )
        if udg_Players>1 and GetRandomInt(1,15)==1 then
            call GeneralText(0.8, "|cfff3ad00Marine|r : Radio check." )
            call PolledWait(2.5)
            call GeneralText(0.8, "|cfff3ad00Marine #2|r : I read you." )
        endif
        call PolledWait(12.*udg_Pace)
        call MultiboardSetTitleText(udg_SquadStatus, "Insertion Complete" )
        if udg_InitialPlayers==1 then //50% less fractures in solo
            loop
                exitwhen i>10
                set udg_AilmentChance[(i*7)+4] = 16
                set i=i+1
            endloop
        endif
        call GeneralText(4.0, "|cffcd950cBattalion|r : Head to Ravenholm and gather intel. The town has some medical supplies." )
        call PolledWait(7.)
        call StartSound(gg_snd_SatelliteImaging)
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0.0, 0.0, 3.0, "|cffcd950cALICE|r : Satellite image of Primary Objective captured." )
        call PingMinimapEx(x,y,10.0,255,0,0,false)
        call SetCameraQuickPosition(x,y)
        call MultiboardSetTitleText( udg_SquadStatus, "Patrol Sector E." )
        set i = 1
        loop
            exitwhen i>udg_Players
            set u = CreateUnit(ConvertedPlayer(udg_Sorted[i]),'e002',x,y,0.0)
            call UnitApplyTimedLife(u,'Bhwd',30.0)
            call TriggerSleepAction(0.)
            set i=i+1
        endloop
        call CameraSetSourceNoise(10.0,0.10)
        call PolledWait(6.)
        call GeneralText(8.0, "|cffcd950cBattalion|r : This is a routine patrol and we don't want nothing fancy. Get this done quick and easy. We have hot chow waiting for you back at base." )
        call PolledWait(10.0*udg_Pace)
        call MBSet( udg_SquadStatus, 1, 1, "Objective : Patrol Perimeter" )
        call ALICE2Text(5.0, "|cff8b864eNew Objective|r : Patrol Perimeter." )
        call SetPlayerHandicap(Player(13),GetPlayerHandicap(Player(13))-0.15)
        call SetPlayerHandicap(Player(14),GetPlayerHandicap(Player(14))-0.15)
        set u = null
        
		call ExecuteRegisteredFunction("Wave1a")
        //call Wave1a.execute()
    endfunction


    private function init takes nothing returns nothing
        local trigger t
        call RegisterFunction("Wave0", function Wave0)
        call RegisterFunction("Wave0.CleanupLeaks", function CleanupLeaks)
        
        set G = CreateGroup()
        set t = CreateTrigger()
		call TriggerRegisterTimerEvent(t,10.0,true)
        call TriggerAddAction(t,function CivilianAI)
		call DisableTrigger(t)
        set CivilianAITrigger = t
		
		set t = CreateTrigger()
		call TriggerRegisterEnterRectSimple( t, gg_rct_Residence41 )
		call TriggerAddAction(t,function CivilianAIFleeAction)
		call DisableTrigger(t)
		set CivilianAIFlee = t 
		
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_ATTACKED )
		call TriggerAddCondition(t, Condition(function CivilianAIShotAtActions))
		call DisableTrigger(t)
		set CivilianAIShotAt = t 
		
    endfunction
    
endlibrary